import MyWebRTC from "./MyWebRTC";

class StreamRTC extends MyWebRTC {
  localStream = null;
  constructor(wsUrl, onStream) {
    super(wsUrl, null, onStream);
  }

  // 发起端通道：实现父类抽象方法
  requestChannel = async (pc) => {
    const myStream = await navigator.mediaDevices.getUserMedia({
      video: true,
      audio: true,
    });
    this.localStream = myStream;
    this.streamCallBack(this.me, myStream);
    // 将媒体流添加到webrtc的音视频收发器
    myStream.getTracks().forEach((track) => {
      pc.addTrack(track, myStream);
    });
  };

  // 响应端通道：实现父类抽象方法
  responseChannel = async (pc) => {
    await this.requestChannel(pc);
  };
}

export default StreamRTC;
